extract year from datetime pandas

40

month from datetime pandas -

#Exctract month and create a dedicated column df["Month"] from a 
#column in datetime format df["Date"]
df['Month'] = pd.DatetimeIndex(df['Date']).month

to extract out only year month from a date column in pandas -

df['month_year'] = df['date_column'].dt.to_period('M')

pandas extract month year from date -

#if the date format comes in datetime, we can also extract the day/month/year using the to_period function
#where 'D', 'M', 'Y' are inputs
df['month_year'] = pd.to_datetime(df['birth_date']).dt.to_period('M')
df.head()

Comments

Submit
0 Comments